package uk.ac.lkl.common.associator.ui;




import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import uk.ac.lkl.common.ui.ViewCreator;


public class AssociationViewPanel<O> extends JPanel {
	private static final long serialVersionUID = 1L;

    private Class<O> modelClass;

    private O model;

    private JPanel leftHandle;

    private JPanel rightHandle;


    public AssociationViewPanel(O model, Class<O> modelClass,
            ViewCreator<O> viewCreator) {
        setLayout(new BorderLayout());
        initialiseHandles();
        this.model = model;
        this.modelClass = modelClass;
        updateView(viewCreator);
    }


    private void initialiseHandles() {
        this.leftHandle = createHandle();
        add(leftHandle, BorderLayout.WEST);
        this.rightHandle = createHandle();
        add(rightHandle, BorderLayout.EAST);
    }


    private JPanel createHandle() {
        JPanel handlePanel = new JPanel();
        handlePanel.setBackground(Color.yellow);
        handlePanel.setBorder(BorderFactory.createEtchedBorder());
        return handlePanel;
    }


    public void setLeftHandleVisible(boolean leftHandleVisible) {
        leftHandle.setVisible(leftHandleVisible);
    }


    public boolean isLeftHandleVisible() {
        return leftHandle.isVisible();
    }


    public void setRightHandleVisible(boolean rightHandleVisible) {
        rightHandle.setVisible(rightHandleVisible);
    }


    public boolean isRightHandleVisible() {
        return rightHandle.isVisible();
    }


    public O getModel() {
        return model;
    }


    public Class getModelClass() {
        return modelClass;
    }


    public void updateView(ViewCreator<O> viewCreator) {
        JPanel viewPanel = new JPanel(new GridLayout(1, 1));
        Component view = viewCreator.createView(model);
        viewPanel.add(view);
        viewPanel.setBorder(BorderFactory.createEtchedBorder());
        add(viewPanel, BorderLayout.CENTER);
    }

}
